package joney.main.action;

import joney.model.DataModel;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;

public class FileActions {

	public static final String SAVE = "Save";
	public static final String SAVE_AS = "Save_as";
	public static final String OPEN = "Open";
	public static final String IMPORT = "Import";
	
	
	public static class Import extends AbstractAction
	{

		public Import(IWorkbenchWindow window) {
			super(window, "&" + IMPORT, IMPORT);
		}

		public void run() {
			FileDialog fd = new FileDialog(window.getShell());
			String path = fd.open();
			if(path != null)
			{
				DataModel.importFile(path);
			}
		}
		
	}
	
	public static class Open extends AbstractAction
	{
		public Open(IWorkbenchWindow window) {
			super(window, "&Open", OPEN);
		}

		public void run() {
			
			FileDialog fd = new FileDialog(window.getShell());
			String path = fd.open();
			if(path != null)
			{
				if(!DataModel.loadFile(path))
				{
					MessageDialog.openError(window.getShell(), "Open", "Error Opening " + path);
				}
				else{
					DataModel.fireExpensesUpdated();
					DataModel.isDirty = true;
				}
			}
		}
		
	}
	
	public static class Save extends AbstractAction
	{
		public Save(IWorkbenchWindow window) {
			super(window, "&Save", SAVE);
			setCtrlAcceleratorKey('S');
		}

		public void run() {
			
			if(!DataModel.saveDefaultFile())
			{
				MessageDialog.openError(window.getShell(), "Save", "Error Saving " + DataModel.DEFAULT_FILE_PATH );
				
			}
			else
				MessageDialog.openInformation(window.getShell(), "Save", "File Saved to " + DataModel.DEFAULT_FILE_PATH );
			
		}
		
	}
	
	public static class SaveAs extends AbstractAction
	{
		public SaveAs(IWorkbenchWindow window) {
			super(window, "&Save As", SAVE_AS);
		}

		public void run() {
			
			FileDialog fd = new FileDialog(window.getShell(), SWT.SAVE);
			String path = fd.open();
			if(path != null)
			{
				DataModel.saveFile(path);
				
				if(DataModel.saveDefaultFile())
				{
					MessageDialog.openInformation(window.getShell(), "Save", "File Saved to " + path );
				}
				else
					MessageDialog.openError(window.getShell(), "Save", "Error Saving " + path );
			}
			
			
			
			
		}
		
	}
	
}
